package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @ClassName SubjectExcelListener
 * @Description TODO
 * @Detail 从Excel读取课程分类并写入数据库
 *              步骤：
 *                  1、读取前端传来的Excel文件
 *                  2、创建一个实体类，用来定义每列的属性和索引
 *                  3、创建一个监听器，继承AnalysisEventListener
 *                      1、由于该监听器需要设置一个eduSubjectService属性，但又不能在该监听器注入EduSubjectService，可以使用构造函数进行注入
 *                  4、逐行读取数据
 *                      1、判断该Excel表是否有数据，如果无数据，则抛出异常
 *                      2、判断一级分类是否存在，如果存在，则跳过，如果不存在，则保存
 *                      3、判断二级分类是否存在，如果存在，则跳过，如果不存在，则保存
 *                  注意：
 *                      1、一级分类的父id默认是0，二级分类的父id默认是一级分类的id
 * @Author MyPC
 * @Date 2020/6/21
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SubjectExcelListener extends AnalysisEventListener<SubjectData>{
    //这里不能注入eduSubjectService，因为SubjectExcelListener无法交给Spring来管理，需要自己new
    private EduSubjectService eduSubjectService;
    //一条一条保存数据，从第一列开始保存，之后第二列，以此类推。。。
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if(subjectData == null){
            throw new GuliException(20001,"数据文件为空");
        }
        //判断一级分类是否已存在
        EduSubject oneEduSubject = existOneSubject(eduSubjectService,subjectData.getOneSubjectName());
        //如果不存在，则保持一级分类
        if(oneEduSubject == null){
            oneEduSubject=new EduSubject();
            oneEduSubject.setParentId("0");
            oneEduSubject.setTitle(subjectData.getOneSubjectName());
            eduSubjectService.save(oneEduSubject);
        }
        //一级分类的id就是二级分类的父id
        String pid=oneEduSubject.getId();
        //查询是否存在二级分类
        EduSubject twoEduSubject = existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), pid);
        //如果不存在，则保存二级分类
        if(twoEduSubject == null){
            twoEduSubject=new EduSubject();
            twoEduSubject.setParentId(pid);
            twoEduSubject.setTitle(subjectData.getTwoSubjectName());
            eduSubjectService.save(twoEduSubject);
        }

    }
    //判断数据库是否已存在一级分类
    private EduSubject existOneSubject(EduSubjectService eduSubjectService,String name){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name).eq("parent_id","0");
        EduSubject eduSubject = eduSubjectService.getOne(wrapper);
        return eduSubject;
    }
    //判断数据库是否已存在二级分类
    private EduSubject existTwoSubject(EduSubjectService eduSubjectService,String name,String pid){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        wrapper.eq("title",name).eq("parent_id",pid);
        EduSubject eduSubject = eduSubjectService.getOne(wrapper);
        return eduSubject;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
